package com.license.logic.geneticAlgorithm.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.license.common.util.Constants;
import com.license.logic.alimentation.Alimentation;
import com.license.logic.alimentation.Food;
import com.license.logic.dao.NutrionDAO;

public class UtilsOperation {
	
	private NutrionDAO nutrionDAO;
	private Map<String, String> foodTypeMap;
	public UtilsOperation(){
		nutrionDAO = new NutrionDAO();
	}
	
	public void initialize(){
		foodTypeMap = nutrionDAO.getClasifiedFoodTypeMap();
	}
	
	public String classifyAlimentation(Alimentation alimentation){
		this.initialize();
		
		Map<String, Integer> alimentationMap = new HashMap<String, Integer>();
		alimentationMap.put(Constants.VEGETABLE, 0);
		alimentationMap.put(Constants.FRUIT, 0);
		alimentationMap.put(Constants.MILK, 0);
		alimentationMap.put(Constants.GRAMINEA, 0);
		alimentationMap.put(Constants.MEAT, 0);
		alimentationMap.put(Constants.OTHER, 0);
		
		this.classifyFoodList(alimentationMap, alimentation.getBreakFast());
		this.classifyFoodList(alimentationMap, alimentation.getDinner());
		this.classifyFoodList(alimentationMap, alimentation.getLunch());
		this.classifyFoodList(alimentationMap, alimentation.getSnack());
		
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(alimentationMap.get(Constants.VEGETABLE)/70);
		stringBuilder.append("," + alimentationMap.get(Constants.FRUIT)/70);
		stringBuilder.append("," + alimentationMap.get(Constants.MEAT)/70);
		stringBuilder.append("," + alimentationMap.get(Constants.MILK)/70);
		stringBuilder.append("," + alimentationMap.get(Constants.GRAMINEA)/70);
		stringBuilder.append("," + alimentationMap.get(Constants.OTHER)/70);
		
		return stringBuilder.toString();
	}
	
	public void classifyFoodList ( Map<String, Integer> classifiedFood, List<Food> foods){
		for (Food food : foods){
			String foodType = this.getFoodType(food);
			classifiedFood.put(foodType, classifiedFood.get(foodType) + food.getPortions() * food.getCharacteristics().getGramsPerPortion());
		}
	}
	
	public String getFoodType(Food food){
		return foodTypeMap.get(food.getName());
	}
}
